# -*- python -*-
# ex: set syntax=python:

# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import config
import master_site_config

from buildbot.scheduler import Periodic
from buildbot.schedulers.basic import AnyBranchScheduler
from buildbot.schedulers.timed import Nightly
from buildbot.status import mail

from master import exception_notifier
from master import gitiles_poller
from master import master_utils
from master import slaves_list
from master.factory import annotator_factory
from master.factory import remote_run_factory


ActiveMaster = master_site_config.InfraCron

c = BuildmasterConfig = {}
c['slavePortnum'] = ActiveMaster.slave_port
c['schedulers'] = []
c['builders'] = []
c['change_source'] = []
c['status'] = []

def mergeRequests(req1, req2):
  if 'publish_tarball' in (req1.buildername, req2.buildername):
    return False
  return req1.canBeMergedWith(req2)
c['mergeRequests'] = mergeRequests

f_annotations = annotator_factory.AnnotatorFactory(ActiveMaster)

INFRA_REPO_URL = 'https://chromium.googlesource.com/infra/infra'

def m_remote_run(recipe, **kwargs):
  return remote_run_factory.RemoteRunFactory(
      active_master=ActiveMaster,
      repository=INFRA_REPO_URL,
      recipe=recipe,
      factory_properties={'path_config': 'kitchen'},
      **kwargs)

####### DATABASE

config.DatabaseSetup(c)


####### CHANGE SOURCES
c['change_source'].extend([
  gitiles_poller.GitilesPoller(INFRA_REPO_URL)
])

####### BUILDERS

c['builders'].extend([
  {
    'name': 'gsubtreed',
    'factory': f_annotations.BaseFactory(
        'infra/gsubtreed',
        {'target_repo': 'https://chromium.googlesource.com/chromium/src'}),
    'auto_reboot' : False,
    'category': '1cron',
  },
  {
    'name': 'gsubtreed-aosp-platform-system-core',
    'factory': f_annotations.BaseFactory(
        'infra/gsubtreed',
        {'target_repo':
             'https://chromium.googlesource.com/aosp/platform/system/core'}),
    'auto_reboot' : False,
    'category': '1cron',
  },
  {
    'name': 'gsubtreed-breakpad',
    'factory': f_annotations.BaseFactory(
        'infra/gsubtreed',
        {'target_repo': 'https://chromium.googlesource.com/breakpad/breakpad'}),
    'auto_reboot' : False,
    'category': '1cron',
  },
  {
    'name': 'gsubtreed-infra',
    'factory': f_annotations.BaseFactory(
        'infra/gsubtreed',
        {'target_repo': 'https://chromium.googlesource.com/infra/infra'}),
    'auto_reboot' : False,
    'category': '1cron',
  },
  {
    'name': 'gsubtreed-llvm-clang',
    'factory': f_annotations.BaseFactory(
        'infra/gsubtreed',
        {'target_repo':
            'https://chromium.googlesource.com/external/'+
            'github.com/llvm-mirror/clang'},
    ),
    'auto_reboot' : False,
    'category': '1cron',
  },
  {
    'name': 'gsubtreed-luci-py',
    'factory': f_annotations.BaseFactory(
        'infra/gsubtreed',
        {'target_repo': 'https://github.com/luci/luci-py'}),
    'auto_reboot' : False,
    'category': '1cron',
  },
  {
    'name': 'gsubtreed-mojo',
    'factory': f_annotations.BaseFactory(
        'infra/gsubtreed',
        {'target_repo': 'https://github.com/domokit/mojo'}),
    'auto_reboot' : False,
    'category': '1cron',
  },
  {
    'name': 'gsubtreed-webrtc',
    'factory': f_annotations.BaseFactory(
        'infra/gsubtreed',
        {'target_repo': 'https://chromium.googlesource.com/external/webrtc'}),
    'auto_reboot' : False,
    'category': '1cron',
  },
  {
    'name': 'lkgr-tag-pusher',
    'factory': f_annotations.BaseFactory('infra/lkgr_tag_pusher'),
    'auto_reboot' : False,
    'category': '1cron',
  },
  {
    'name': 'publish_tarball',
    'factory': m_remote_run('publish_tarball', max_time=3*60*60),
    'auto_reboot' : False,
    'category': '1cron',
  },
  {
    'name': 'rebaseline-o-matic',
    'factory': m_remote_run('rebaseline_o_matic'),
    'auto_reboot' : False,
    'category': '1cron',
  },
  {
    'name': 'GAE SDK CIPD Packager',
    'factory': f_annotations.BaseFactory(recipe='infra/gae_sdk_cipd_packager'),
    'category': '1cron',
  },
  {
    'name': 'git-hash-metrics',
    'factory': m_remote_run(recipe='git_hash_metric'),
    'category': '3monitoring',
  },
  {
    'name': 'wpt-exporter',
    'factory': m_remote_run('wpt_export'),
    'auto_reboot' : True,
    'category': '1cron',
  },
  {
    'name': 'wpt-importer',
    'factory': m_remote_run('wpt_import'),
    'auto_reboot' : True,
    'category': '1cron',
  },
])

third_party_platforms = (
  'linux-amd64',
  'linux-386',
  'mac-amd64',
  'windows-amd64',
  'windows-386',
)
third_party_builders = [
  {
    'name': 'third-party-packages-%s' % p,
    'factory': m_remote_run('third_party_packages'),
    'auto_reboot' : False,
    'category': '1cron',
  }
  for p in third_party_platforms
]
c['builders'].extend(third_party_builders)

####### SCHEDULERS

c['schedulers'].extend([
  # Period is intentionally small so that if a builder fails fast, it also
  # gets restarted fast.
  Periodic(
      name='infra-runner-scheduler',
      periodicBuildTimer=1*60,
      builderNames=[
          'gsubtreed',
          'gsubtreed-aosp-platform-system-core',
          'gsubtreed-breakpad',
          'gsubtreed-infra',
          'gsubtreed-llvm-clang',
          'gsubtreed-luci-py',
          'gsubtreed-mojo',
          'gsubtreed-webrtc',
          'lkgr-tag-pusher',
          'rebaseline-o-matic',
          'wpt-exporter',
      ]
  ),
  Nightly(
      name='publish_tarball',
      branch=None,
      builderNames=['publish_tarball'],
      hour=range(0, 24, 3),
  ),
  Nightly(
      name='wpt-importer-scheduler',
      branch='master',
      hour=[6],
      minute=0,
      builderNames=['wpt-importer']),
  Nightly(
      name='gae-sdk-scheduler',
      branch='master',
      hour=range(0, 24, 4),
      minute=0,
      builderNames=['GAE SDK CIPD Packager']),
  Nightly(
      name='third-party-packages',
      branch='master',
      hour=range(0, 24, 4),
      minute=0,
      builderNames=[b['name'] for b in third_party_builders]),

  # Trigger this builder on any change anywhere.
  AnyBranchScheduler(
      name='git-hash-metrics',
      builderNames=['git-hash-metrics']),
])


####### BUILDSLAVES

# Associate the slaves to the builders. The configuration is in slaves.cfg.
slaves = slaves_list.SlavesList('slaves.cfg', 'InfraCron')
for builder in c['builders']:
  builder['slavenames'] = slaves.GetSlavesName(builder=builder['name'])

# The 'slaves' list defines the set of allowable buildslaves. List all the
# slaves registered to a builder. Remove dupes.
c['slaves'] = master_utils.AutoSetupSlaves(
    c['builders'], config.Master.GetBotPassword())

# Make sure everything works together.
master_utils.VerifySetup(c, slaves)

####### STATUS TARGETS

# Adds common status and tools (web status, mail notifier) to this master.
master_utils.AutoSetupMaster(
    c, ActiveMaster, mail_notifier=False, order_console_by_time=True,
    public_html='../master.chromium/public_html',
    templates=['templates', '../master.chromium/templates']
)

# Send alerts when builders fail.

c['status'].extend([
  mail.MailNotifier(
      fromaddr=ActiveMaster.from_address,
      builders=['rebaseline-o-matic'],
      subject='rebaseline-o-matic failed',
      mode='failing',
      relayhost=config.Master.smtp,
      sendToInterestedUsers=False,
      extraRecipients=['blink-infra+alerts@google.com',
                       'chrome-troopers+alerts@google.com'],
  ),
  mail.MailNotifier(
      fromaddr=ActiveMaster.from_address,
      builders=[b['name'] for b in third_party_builders],
      subject='third-party-packages build failed',
      mode='failing',
      relayhost=config.Master.smtp,
      sendToInterestedUsers=False,
      extraRecipients=['luci-eng@google.com'],
  ),
])

####### PROJECT IDENTITY

c['projectName'] = ActiveMaster.project_name
c['buildbotURL'] = ActiveMaster.buildbot_url
